package cn.dyw.cloudmusic.config;

import lombok.extern.log4j.Log4j;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author: dyw770
 * @Date: 2018/4/11
 */
@Log4j
public class OkHttpConfig {

    private static OkHttpClient okHttpClient;

    static {
        okHttpClient = new OkHttpConfig().okHttpClient();
    }

    public static OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    private OkHttpClient okHttpClient() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(
                message -> {
                    if (log.isDebugEnabled()) {
                        log.debug(message);
                    } else {
                        log.info(message);
                    }
                }
        );
//        if (log.getLevel().equals(Level.DEBUG)) {
//            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        } else if (log.getLevel().equals(Level.INFO)){
//            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
//        } else {
//            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
//        }
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .addNetworkInterceptor(httpLoggingInterceptor)
                .retryOnConnectionFailure(true)
                .sslSocketFactory(Objects.requireNonNull(getTrustedSSLSocketFactory()), x509TrustManager)
                .hostnameVerifier(DO_NOT_VERIFY);
        return builder.build();
    }

    private X509TrustManager x509TrustManager = new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        public void checkClientTrusted(
                X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(
                X509Certificate[] certs, String authType) {
        }
    };

    private TrustManager[] trustAllCerts = new TrustManager[]{
            x509TrustManager
    };

    private HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true;

    private SSLSocketFactory getTrustedSSLSocketFactory() {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            return sc.getSocketFactory();
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    private OkHttpConfig() {
    }
}
